<template>
  <div class="app_user h100" v-loading="loading">
    <div class="pageContent">
      <el-row :gutter="11" class="fileSearch">
        <el-form
          :model="ruleForm"
          :inline="true"
          ref="ruleForm"
          label-width="80px"
        >
          <el-form-item label="用户昵称:" prop="nickname">
            <el-input v-model="ruleForm.nickname" clearable></el-input>
          </el-form-item>
          <el-form-item label="手机号:" prop="phone">
            <el-input v-model="ruleForm.phone" clearable></el-input>
          </el-form-item>
          <el-form-item label="状态:" prop="state">
            <el-select v-model="ruleForm.state" placeholder="请选择纪念馆状态">
              <el-option label="全部" value=""></el-option>
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              @click="onSubmit(1)"
            >
              查询
            </el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        :data="tableData"
        border
        :header-cell-style="{
          background: 'rgb(246,247,251)',
          color: '#606266'
        }"
        stripe
        class="defaultTableHeight"
      >
        <el-table-column type="index" label="编号" width="50">
        </el-table-column>
        <el-table-column prop="nickname" align="center" label="用户昵称">
        </el-table-column>
        <el-table-column prop="phone" align="center" label="手机号">
        </el-table-column>
        <el-table-column
          prop="sacrificeCoinNum"
          align="center"
          label="祭祀币个数"
        >
        </el-table-column>
        <el-table-column
          prop="commemorateCoinNum"
          align="center"
          label="纪念币个数"
        >
        </el-table-column>
        <el-table-column align="center" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.gmtCreate | timestampToTime }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="性别">
          <template slot-scope="scope">
            <!-- 1为男 -1 为女 -->
            {{
              scope.row.gender == 1
                ? "男"
                : scope.row.gender == -1
                ? "女"
                : "未知"
            }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="头像">
          <template slot-scope="scope">
            <el-image
              class="avatarUrl_img"
              :src="scope.row.avatarUrl"
              alt=""
              :preview-src-list="[scope.row.avatarUrl]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" label="操作" width="280">
          <template slot-scope="scope">
            <!-- {{ scope.row.state }}{{ scope.row.state === 0 ? "禁用" : "启用" }} -->
            <el-button
              :type="
                scope.row.userType === 'normal' || scope.row.userType === ''
                  ? 'primary'
                  : 'danger'
              "
              size="mini"
              @click="changeType(scope.row)"
              >{{
                scope.row.userType === "normal" || scope.row.userType === ""
                  ? "设为管理员"
                  : "取消管理员"
              }}
            </el-button>
            <el-button
              v-if="scope.row.state === 1"
              type="danger"
              size="mini"
              @click="isDisable(scope.row)"
            >
              禁用
            </el-button>
            <el-button
              v-if="scope.row.state === 0"
              type="primary"
              size="mini"
              @click="changeState(scope.row)"
            >
              启用
            </el-button>
            <el-button
              type="success"
              size="mini"
              @click="getdetails(scope.row.id)"
            >
              操作详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <p style="text-align: right; ">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pager.currentPage"
          :page-size="pager.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          background
          layout="total,sizes,prev, pager, next, jumper"
          :total="pager.total"
        >
        </el-pagination>
      </p>
    </div>
    <el-dialog
      title="是否禁用"
      :visible.sync="dialogVisible"
      width="600px"
      v-dialogDrag
      @close="closeDialog"
    >
      <el-form
        ref="goodsForm"
        :model="goodsForm"
        :rules="goodsFormRules"
        label-width="80px"
      >
        <el-form-item label="禁用原因" prop="disableCause">
          <el-input
            ref="inputTitle"
            v-model="goodsForm.disableCause"
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">确认禁用</el-button>
          <el-button @click="closeDialog">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- // 操作详情弹窗 -->
    <el-dialog
      title="操作详情"
      :visible.sync="dialogVisibleDetails"
      width="600px"
      v-dialogDrag
      @close="dialogVisibleDetails = false"
    >
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in detailsList"
          :key="index"
          :timestamp="item.gmtCreate | timestampToTime"
          :type="item.state == 0 ? 'danger ' : 'success'"
          placement="top"
        >
          <el-card>
            <h4>操作账号 :{{ item.realname }}</h4>
            <p v-if="item.state == 1">启用</p>
            <p v-if="item.state == 0">禁用的原因: {{ item.disableCause }}</p>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserRecord,
  userManage_disables,
  userManage_type,
  userManage_selectUserState
} from "@/api/appUser/index";
export default {
  name: "appUserList",
  data() {
    return {
      loading: false,
      dialogVisible: false, // 禁用弹窗
      dialogVisibleDetails: false, // 详情弹窗
      ruleForm: {
        nickname: "",
        phone: "",
        state: ""
      },
      pager: {
        pageSize: 20,
        total: 1,
        currentPage: 1
      },
      tableData: [],
      goodsForm: {
        disableCause: "",
        id: "",
        state: ""
      },
      goodsFormRules: {
        disableCause: [
          { required: true, message: "请输入禁用原因", trigger: "blur" }
        ]
      },
      detailsList: []
    };
  },
  mounted() {
    this.onSubmit();
  },
  //注销window.onresize事件
  destroyed() {},
  methods: {
    async onSubmit(val) {
      if (val) {
        this.pager.currentPage = 1;
      }
      this.loading = true;
      let postData = {
        nickname: this.ruleForm.nickname,
        phone: this.ruleForm.phone,
        state: this.ruleForm.state,
        pageSize: this.pager.pageSize,
        currentPage: this.pager.currentPage
        // orderField: "", //排序字段
        // orderType: "" //排序顺序
      };
      console.log(postData, "postData");
      try {
        const res = await getUserRecord(postData);
        this.loading = false;
        console.log(res, 200);
        if (res.code !== "2000") {
          this.$message(res.msg ? res.msg : "数据读取失败");
          return;
        }
        this.tableData = res.data;
        this.pager.pageSize = res.pageSize;
        this.pager.total = res.total;
        this.pager.currentPage = res.currentPage;

        //数据读取成功
      } catch (err) {
        this.loading = false;
        console.log(err, 404);
        this.$message.error(err.msg);
        this.tableData = [];
      }
    },
    // 启用禁用
    async changeState(paramet) {
      this.loading = true;
      let state = paramet.state == 0 ? 1 : 0;
      let postData = {
        id: paramet.id,
        state: state
      };
      if (!state) {
        // 禁用
        postData.disableCause = paramet.disableCause;
      }
      console.log(postData, "postData");
      try {
        const res = await userManage_disables(postData);
        console.log(res, 200);
        this.loading = false;
        if (res.code !== "2000") {
          this.$message.error(res.msg ? res.msg : "操作失败");
          return;
        }
        this.$message.success("操作成功");
        this.dialogVisible = false;
        this.onSubmit();
      } catch (err) {
        this.loading = false;
        console.log(err, 404);
        this.$message.error(err.msg);
      }
    },
    // 打开禁用弹窗
    isDisable(data) {
      this.goodsForm.id = data.id;
      this.goodsForm.state = data.state;
      this.dialogVisible = true;
    },
    // 获取操作详情
    async getdetails(id) {
      this.detailsList = [];
      let postData = {
        id
      };
      console.log(postData, "postData");
      try {
        const res = await userManage_selectUserState(postData);
        console.log(res, 200);
        if (res.code === "2000") {
          if (res.data.length <= 0) {
            this.$message("暂无操作记录");
            return;
          }
          this.detailsList = res.data;
          this.dialogVisibleDetails = true;
          return;
        }
        this.$message.fail(res.msg || "数据请求失败");
      } catch (err) {
        console.log(err, 404);
        this.$message.fail(err.msg);
      }
    },

    submitForm() {
      this.$refs["goodsForm"].validate(valid => {
        console.log(valid, "valid");
        if (valid) {
          this.changeState(this.goodsForm);
        }
      });
    },
    // 设置管理员权限
    async changeType(paramet) {
      this.loading = true;
      let userType =
        paramet.userType == "normal" || paramet.userType == ""
          ? "manage"
          : "normal";
      let postData = {
        id: paramet.id,
        userType: userType
      };
      console.log(postData, "postData");
      try {
        const res = await userManage_type(postData);
        console.log(res, 200);
        this.loading = false;
        if (res.code !== "2000") {
          this.$message.error(res.msg ? res.msg : "操作失败");
          return;
        }
        this.$message.success("操作成功");
        this.onSubmit();
      } catch (err) {
        this.loading = false;
        console.log(err, 404);
        this.$message.error(err.msg);
      }
    },
    // 关闭弹窗的回调
    closeDialog() {
      this.$refs["goodsForm"].resetFields();
      this.dialogVisible = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs["ruleForm"].resetFields();
      this.onSubmit();
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange: function(size) {
      this.pager.pageSize = size;
      this.onSubmit();
    },
    handleCurrentChange: function(currentPage) {
      this.pager.currentPage = currentPage;
      this.onSubmit(); //点击第几页
    }
  }
};
</script>

<style lang="scss" scoped>
.avatarUrl_img {
  width: 70px;
  height: 70px;
}
.el-button {
  margin: 2px;
}
.el-dialog {
  width: 100px;
}
</style>
